<?php
class Tender_Join_ContactController extends Agitum_Controller_Action
{
	protected $_redirector = null;

	public function init() {
		$this->_redirector = $this->_helper->getHelper('Redirector');
	}


	public function indexAction()
	{
		$form = $this->_helper->formLoader('Contact');
		if ( $this->_request->isPost() ) {
			$params = $this->_request->getPost();
			if ( $form->isValid($params) ) {

				$begin = new Zend_Session_Namespace('tender/begin');
				$wishes = new Zend_Session_Namespace('tender/wishes');
				$contact = new Zend_Session_Namespace('tender/contact');
				foreach ( $params as $key => $value ) {
					$contact->$key = $value;
				}

				$db = Zend_Registry::get('Zend_Db');
				$db->beginTransaction();
				try {

					$user = Zend_Registry::get('Logged_User');
					if ( $user->id == Agitum_Model_User::GUEST_USER_ID ) 
					{
						$user = new Agitum_Model_User();
						$user->name = $contact->phone;
						$user->is_active = Agitum_Model_User::ENABLED;
						$user->email = $contact->email;
		
						if ( !$user->find() ) {
							// 1. создать пользователя
							$user->role = 'tender';
							$user->password = (string)crc32(microtime(true));
							$user->phone = $contact->phone;
							$user->firstname = $contact->firstname;
							$user->lastname = $contact->lastname;
							$user->middlename = $contact->middlename;
							$user->save();
						}
						
						// 2. авторизовать пользователя
						$auth = Zend_Auth::getInstance();
						$auth->authenticate($user);
						echo '!!!!!!!!!!!!!!!!!!!!!!';
					}
					
					// 3. создать заявку
					$tender = new Agitum_Model_Tender();
					$tender->owner_id = $user->id;
					$tender->city_id = $begin->city_id;
					list($tender->date_start, $tender->date_stop) = explode(' - ', $begin->daterange);
					$tender->budget = $begin->budget;
					$tender->budget_common = $begin->budget_type;
					$tender->budget_start = $begin->budget_range_start;
					$tender->budget_stop = $begin->budget_range_stop;
					$tender->adult = $begin->adult;
					$tender->children = $begin->children;
					$tender->days = $begin->days;
					$tender->days_choosed = $begin->days_choosed;
					$tender->stars = $wishes->stars;
					$tender->visa_id = $wishes->visa;
					$tender->extra = $wishes->extra;
					
					$start = time();
					$stop = $start + ($contact->hours * 3600);
					
					$tender->start = date('Y-m-d H:i:s', $start);
					$tender->stop = date('Y-m-d H:i:s', $stop);
					
					$tender->setTour( array($wishes->tour_off, $wishes->tour_on) );
					$tender->setCountry( array($wishes->country_off_id, $wishes->country_on_id) );
					
					$tender->save();
					$db->commit();
					
					$begin->unsetAll();
					$wishes->unsetAll();
					$contact->unsetAll();
					
					$final = new Zend_Session_Namespace('final');
					$final->id = $tender->id;
				
					$param = array('city' => 0, 'country' => 0, 'tour' => 0);
					$tag = new Agitum_Model_Cache_Tag_Tender_List($param);
					$tag->clean();
					
					$this->_redirector->gotoUrl('/tender/final');
				} catch (Zend_Db_Table_Exception $e) {
				    echo 'xxxxxx';
				    $db->rollBack();
					throw new Exception($e->getMessage(), $e->getCode());
				} catch (Zend_Db_Exception $e) {
				    echo 'yyyyy';
				    $db->rollBack();
				    throw new Exception($e->getMessage(), $e->getCode());
				} catch (Zend_Db_Statement_Exception $e) {
				    echo 'zzzzz';
				    $db->rollBack();
				    throw new Exception($e->getMessage(), $e->getCode());
				}
			}
		}
		$this->view->form = $form;
	}

	public function validateAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();

		$form = $this->_helper->formLoader('Contact');
		$form->isValidPartial($this->_request->getPost());
		$json = $form->getMessages();

		$this->_response->setHeader('Content-Type', 'application/json');
		echo Zend_Json::encode($json);
	}

}